﻿namespace Resharper.Abstractions.CodeDom.TypeMembers.Indexers
{
  using System.Collections.Generic;
  using System.Linq;
  using JetBrains.Annotations;
  using JetBrains.ReSharper.Psi.CSharp.Tree;
  using Resharper.Abstractions.CodeDom.Accessors;

  /// <summary>Defines the <see cref="Indexer"/> class.</summary>
  public class Indexer : TypeMember
  {
    #region Constructors and Destructors

    internal Indexer([NotNull] IIndexerDeclaration property) : base(property)
    {
    }

    #endregion

    #region Properties

    /// <summary>
    /// Gets the accessors.
    /// </summary>
    public IEnumerable<Accessor> Accessors
    {
      get
      {
        return this.InternalIndexer.AccessorDeclarations.Select(a => new Accessor(a));
      }
    }

    /// <summary>
    /// Gets the get accessor.
    /// </summary>
    [CanBeNull]
    public Accessor Getter
    {
      get
      {
        return this.Accessors.FirstOrDefault(a => a.IsGetter);
      }
    }

    /// <summary>
    /// Gets the set accessor.
    /// </summary>
    [CanBeNull]
    public Accessor Setter
    {
      get
      {
        return this.Accessors.FirstOrDefault(a => a.IsSetter);
      }
    }

    /// <summary>
    /// Gets or sets the internal function.
    /// </summary>
    /// <value>The internal function.</value>
    [NotNull]
    internal IIndexerDeclaration InternalIndexer
    {
      get
      {
        return (IIndexerDeclaration)this.InternalTreeNode;
      }
    }

    #endregion
  }
}